import request, { ApiResponse } from '@/utils/service/request'
import {
  AddRoleReq,
  DeleteRoleReq,
  EditRoleReq,
  GetRoleListReq,
  RoleItem,
  UpdateRolePermReq,
} from '@/api/userCenter/authority/model/roleModel'
import { CommontListResp } from '@/api/types'

export enum RolesApi {
  /** 获取角色列表 */
  GET_ROLE_LIST = '/user-center/role/selectByKeyForPages',
  /** 新增角色 */
  INSERT_ROLE = '/user-center/role/insert',
  /** 删除角色 */
  DELETE_ROLE = '/user-center/role/delete',
  /** 编辑角色 */
  EDIT_ROLE = '/user-center/role/update',
  /** 更新角色和权限的绑定关系 */
  UPDATE_ROLE_PERM = '/user-center/role/updateRolePerm',
  /** 查询角色所拥有权限 */
  QUERY_ROLE_PERM = '/user-center/role/getPermsByRoleId',
}

export function fetchRoleList(body: GetRoleListReq) {
  return request.post<ApiResponse<CommontListResp<RoleItem>>>(
    RolesApi.GET_ROLE_LIST,
    body
  )
}

export function insertRole(body: AddRoleReq) {
  return request.post<ApiResponse<RoleItem>>(RolesApi.INSERT_ROLE, body)
}

export function modifyRole(body: EditRoleReq) {
  return request.post<ApiResponse<null>>(RolesApi.EDIT_ROLE, body)
}

export function deleteRole(body: DeleteRoleReq) {
  return request.post<ApiResponse<null>>(RolesApi.DELETE_ROLE, body)
}

export function updateRolePerm(body: UpdateRolePermReq) {
  return request.post<ApiResponse<null>>(RolesApi.UPDATE_ROLE_PERM, body)
}

export function queryRolePerm(roleId: string) {
  return request.get<ApiResponse<string[]>>(RolesApi.QUERY_ROLE_PERM, {
    params: {
      roleId,
    },
  })
}
